-
Notifications
You must be signed in to change notification settings - Fork 9
Adds pytest_markdown_docs_markdown_it for custom markdownit parser #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for the contribution! I like the pytest hook pattern for this, since the markdown flavor is probably going to be the same across a codebase anyways. |
src/pytest_markdown_docs/plugin.py
Outdated
@@ -123,15 +124,20 @@ def get_prefixed_strings( | |||
|
|||
|
|||
def extract_fence_tests( | |||
markdown_it_parsers: typing.List["MarkdownIt"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be missing something here, but why is this a list? (seems like we are throwing away anything except the first element anyway)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pytest returns a list because the hook can be defined multiple times. For example:
tests/
conftest.py <- hook can be defined here
io/
conftest.py <- same hook can be defined here
my_test.py
The default is to have a list that contains all the hooks and the nested conftest.py
gets called first so they are first in the hooks list.
I simplified the code by using @pytest.hookspec(firstresult=True)
, which returns the first one by default: 0be6a10
Also, 5fe7f6f, which defines a default MarkdownIt
parser using the hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, yeah this makes sense! Thanks for the explanation and I like the firstresult and default implementation
src/pytest_markdown_docs/plugin.py
Outdated
@@ -294,8 +300,13 @@ def find_object_tests_recursive( | |||
or "<Unnamed obj>" | |||
) | |||
fence_syntax = FenceSyntax(self.config.option.markdowndocs_syntax) | |||
markdown_it_parsers = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the list comment above - maybe it's better to validate the hook return value here (making sure it's at most one specification, since that's what we support) and then just pass in the single MarkdownIt we want to use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the other changes I don't think we need to check for anything here 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, thanks!
Issue: Fixes #31
This PR adds a
pytest_markdown_docs_markdown_it
pytest
hook to allow developers to customize the markdown_it parser. This means they can add plugins such asadmon_plugin
to the parser.An alternative is to add something like https://github.com/modal-labs/pytest-markdown-docs/blob/main/src/pytest_markdown_docs/_runners.py, but using a
pytest
hook feels a little more natural with how pytest does things. For example,pytest-xdist
has uses apytest_xdist_make_scheduler
to configure a custom scheduler:https://github.com/pytest-dev/pytest-xdist/blob/f83299cd646965ac0560adddcf7204adde9f486e/src/xdist/newhooks.py#L96-L100